import request from '@/utils/request';
import { getToken } from '@/utils/auth';
import Vue from 'vue'
import Qs from 'qs'

// 用户头像上传
export function uploadFile(url, data) {
  return request({
    url,
    method: 'post',
    data: data /*Qs.stringify(data)*/,
    headers: {
      type: 'form-data',
      'Content-Type': 'multipart/form-data;charset=utf-8',
    }
  })
}

export const commonList = (url, method, data, type) => {
  return new Promise((resolve, reject) => {
    var obj = {
      url: url,
      method: method,
      headers: {
        'Content-Type': 'application/x-www-form-urlencoded'
      },
      transformRequest: [function (data, headers) {
        return Qs.stringify(data);
      }]
    };
    // type有值则是FormData请求方式，没有就是json格式
    if (!type) {
      delete obj.headers;
      delete obj.transformRequest;
    }
    (method == 'get' || method == 'GET') ? obj.params = data : obj.data = data

    request(obj).then(res => {
      if (res && (res.code == 0 || res.code == 200)) {
        resolve(res);
      } else {
        Vue.prototype.msgError(res.msg);
        reject(res)
      }
    }).catch(error => {
      // Vue.prototype.msgError(error)
      reject(error)
    });
  })
}

export const getDetailData = (url, method, data, type) => {
  return new Promise((resolve, reject) => {
    var obj = {
      url: url,
      method: method,
      headers: {
        'Content-Type': 'application/x-www-form-urlencoded'
      },
      transformRequest: [function (data, headers) {
        return Qs.stringify(data);
      }]
    };
    // type有值则是FormData请求方式，没有就是json格式
    if (!type) {
      delete obj.headers;
      delete obj.transformRequest;

    }
    (method == 'get' || method == 'GET') ? obj.params = data : obj.data = data

    request(obj).then(res => {
      if (res && (res.code == 0 || res.code == 200)) {
        resolve(res);
      } else {
        Vue.prototype.msgError(res.msg)
        reject('error')
      }
    })
  })
}

export const operateData = (url, method, data, type) => {
  return new Promise((resolve, reject) => {
    var obj = {
      url: url,
      method: method,
      headers: {
        'Content-Type': 'application/x-www-form-urlencoded'
      },
      transformRequest: [function (data, headers) {
        return Qs.stringify(data);
      }]
    };
    // type有值则是FormData请求方式，没有就是json格式
    if (!type) {
      delete obj.headers;
      delete obj.transformRequest;

    }
    (method == 'get' || method == 'GET') ? obj.params = data : obj.data = data

    request(obj).then(res => {
      if (res && (res.code == 0 || res.code == 200)) {
        Vue.prototype.msgSuccess(res.msg)
        resolve(res);
      } else {
        Vue.prototype.msgError(res.msg)
        reject('error')
      }
    })
  })
}
export const checkUniq = (url, method, data, type) => {
  return new Promise((resolve, reject) => {
    var obj = {
      url: url,
      method: method,
      headers: {
        'Content-Type': 'application/x-www-form-urlencoded'
      },
      transformRequest: [function (data, headers) {
        return Qs.stringify(data);
      }]
    };
    // type有值则是FormData请求方式，没有就是json格式
    if (!type) {
      delete obj.headers;
      delete obj.transformRequest;

    }
    (method == 'get' || method == 'GET') ? obj.params = data : obj.data = data

    request(obj).then(res => {
      resolve(res);
    })
  })
}

// 接口校验字段类
export const checkUnqform = (url, rule, value, obj, currentRow, callback) => {
  if (value == '' || value == currentRow[rule.field]) {
    return callback()
  } else {
    obj[rule.field] = value
    checkUniq(url, 'post', obj, 1).then(
      res => {
        if (res == 0 || res == true || res.code == '0' || res.status == '0') {
          return callback()
        } else if (res == 1 || res.status == '1' || res == false) {
          return callback(new Error(value + "已存在"))
        } else if (res.code == '1') {
          return callback()
        }
        else {
          return callback(new Error('系统错误！'))
        }
      }
    )
  }
}



